package com.woniuxy.system.service.impl;

import com.woniuxy.system.demol.entity.Perms;
import com.woniuxy.system.demol.entity.Role;
import com.woniuxy.system.demol.entity.SystemSector;
import com.woniuxy.system.demol.entity.User;
import com.woniuxy.system.mapper.PermsMapper;
import com.woniuxy.system.mapper.RoleMapper;
import com.woniuxy.system.mapper.SystemSectorMapper;
import com.woniuxy.system.mapper.UserMapper;
import com.woniuxy.system.service.PermsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author why
 * @since 2023-09-08
 */
@Service
public class PermsServiceImpl extends ServiceImpl<PermsMapper, Perms> implements PermsService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private PermsMapper permsMapper;

    @Resource
    private SystemSectorMapper systemSectorMapper;

    @Resource
    private RoleMapper roleMapper;

    @Override
    public List<Perms> findByUid(Integer i) {
        //根据用户的id，查看所属部门的id
        User user = userMapper.selectById(i);

        //获取用户的部门id
        String sid = user.getSid();

        //转换int
        int newSid = Integer.valueOf(sid).intValue();

        //根据sid查看权限id
        List<Perms> list = permsMapper.findBySid(newSid);
        return list;
    }

    @Override
    public List<Perms> findAllPerms(String account) {
        //根据account查询部门id
        User byAcc = userMapper.findByAcc(account);
        String getSid = byAcc.getSid();
        int sid = Integer.valueOf(getSid).intValue();

        if (sid==1){
            List<Perms> permsList =new ArrayList<>();
            List<Integer> list = new ArrayList<>();
            list.add(3);
            list.add(5);
            list.add(7);
            list.add(9);
            list.add(11);
            list.add(13);
           for (int i =0 ;i<list.size();i++){
               List<Perms> bySidGen = permsMapper.findBySidGen(list.get(i));
               for (Perms p :
                       bySidGen) {
                   if (!Objects.isNull(p)){
                       permsList.add(p);
                   }
               }
           }
           return permsList;
        }else{

            //根据sid获取部门名字
            SystemSector systemSector = systemSectorMapper.selectById(sid);
            String str = systemSector.getName();
//            str = str.substring(0, str.length() - 1); // 剔除最后一个字符
//            String name =str +"m";

            //根据部门名字查询该角色id  rid
            Role role = roleMapper.selectByName(str);
            Integer id = role.getId();

            //根据rid去查询对应的权限
            List<Perms> bySid  =   permsMapper.findBySidGen(id);

            return  bySid;
        }

    }

    @Override
    public List<Perms> findAllPermsSp(String account) {
        //根据account查询部门id
        User byAcc = userMapper.findByAcc(account);
        String getSid = byAcc.getSid();
        int sid = Integer.valueOf(getSid).intValue();

        if (sid==1){
            //先查找所有的权限
            List<Perms> allPermsList = permsMapper.selectList(null);
            List<Perms> permsList =new ArrayList<>();
            List<Integer> list = new ArrayList<>();
            list.add(3);
            list.add(5);
            list.add(7);
            list.add(9);
            list.add(11);
            list.add(13);
            for (int i =0 ;i<list.size();i++){
                List<Perms> bySidGen = permsMapper.findBySidGen(list.get(i));
                for (Perms p :
                        bySidGen) {
                    System.out.println(p);
                    if (!Objects.isNull(p)){
                        permsList.add(p);
                    }
                }
            }
            for (int m = 0; m < allPermsList.size(); m++) {
                Integer id = allPermsList.get(m).getId();
                for (int n = 0; n < permsList.size(); n++) {
                    Integer id1 = permsList.get(n).getId();
                    if (id.equals(id1)){
                        allPermsList.remove(m);
                    }
                }
            }
            return allPermsList;
        }else{
            //根据sid获取部门名字
            SystemSector systemSector = systemSectorMapper.selectById(sid);
            String str = systemSector.getName();

            //根据部门名字查询该角色id  rid
            Role role = roleMapper.selectByName(str);
            Integer id = role.getId();
            //根据rid去查询对应的权限
            List<Perms> bySid  =   permsMapper.findBySidGen(id);

            String  a = str.substring(0, str.length() - 1); // 剔除最后一个字符
            String name =a +"m";
            Role role1 = roleMapper.selectByName(name);
            Integer id1 = role1.getId();
            List<Perms> bySidGen = permsMapper.findBySidGen(id1);
            for (int i =  bySidGen.size()-1; i >=0 ; i--) {
                Integer id2 = bySidGen.get(i).getId();
                for (int j = bySid.size()-1; j >=0 ; j--) {
                    Integer id3 = bySid.get(j).getId();
                    System.out.println(id2.equals(id3));
                  if (id2.equals(id3)){
                      bySidGen.remove(i);
                  }
                }
            }

            return  bySidGen;
        }
    }
}
